package com.design.mode.single;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-04-03 15:47:04
 */
public class Single01 implements Serializable {

    /**
     * Constructor<? extends Single01> constructor = cls.getDeclaredConstructor();
     * constructor.setAccessible(true);
     * Single01 s2 = constructor.newInstance();
     * <p>
     * 解决通过类实例破解单例问题
     */
    private Single01() {
        if (instance != null) {
            throw new RuntimeException("禁止反序列化");
        }
    }

    private static final Single01 instance = new Single01();

    public static Single01 getInstance() {
        return instance;
    }

    /**
     * 解决 ObjectOutputStream/ObjectInputStream 流反序列化问题
     */
    Object readResolve() throws ObjectStreamException {
        return instance;
    }
}
